home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / make.info-4 (.txt) < prev    next >
GNU Info File  |  1993-05-16  |  50KB  |  977 lines

  1. This is Info file make.info, produced by Makeinfo-1.54 from the input
  2. file make.texinfo.
  3.    This file documents the GNU Make utility, which determines
  4. automatically which pieces of a large program need to be recompiled,
  5. and issues the commands to recompile them.
  6.    This is Edition 0.42, last updated 14 May 1993, of `The GNU Make
  7. Manual', for `make', Version 3.66 Beta.
  8.    Copyright (C) 1988, '89, '90, '91, '92, '93 Free Software
  9. Foundation, Inc.
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice are
  12. preserved on all copies.
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided also
  15. that the section entitled "GNU General Public License" is included
  16. exactly as in the original, and provided that the entire resulting
  17. derived work is distributed under the terms of a permission notice
  18. identical to this one.
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the text of the translations of the section
  22. entitled "GNU General Public License" must be approved for accuracy by
  23. the Foundation.
  24. File: make.info,  Node: Computed Names,  Prev: Substitution Refs,  Up: Advanced
  25. Computed Variable Names
  26. -----------------------
  27.    Computed variable names are a complicated concept needed only for
  28. sophisticated makefile programming.  For most purposes you need not
  29. consider them, except to know that making a variable with a dollar sign
  30. in its name might have strange results.  However, if you are the type
  31. that wants to understand everything, or you are actually interested in
  32. what they do, read on.
  33.    Variables may be referenced inside the name of a variable.  This is
  34. called a "computed variable name" or a "nested variable reference".
  35. For example,
  36.      x = y
  37.      y = z
  38.      a := $($(x))
  39. defines `a' as `z': the `$(x)' inside `$($(x))' expands to `y', so
  40. `$($(x))' expands to `$(y)' which in turn expands to `z'.  Here the
  41. name of the variable to reference is not stated explicitly; it is
  42. computed by expansion of `$(x)'.  The reference `$(x)' here is nested
  43. within the outer variable reference.
  44.    The previous example shows two levels of nesting, but any number of
  45. levels is possible.  For example, here are three levels:
  46.      x = y
  47.      y = z
  48.      z = u
  49.      a := $($($(x)))
  50. Here the innermost `$(x)' expands to `y', so `$($(x))' expands to
  51. `$(y)' which in turn expands to `z'; now we have `$(z)', which becomes
  52.    References to recursively-expanded variables within a variable name
  53. are reexpanded in the usual fashion.  For example:
  54.      x = $(y)
  55.      y = z
  56.      z = Hello
  57.      a := $($(x))
  58. defines `a' as `Hello': `$($(x))' becomes `$($(y))' which becomes
  59. `$(z)' which becomes `Hello'.
  60.    Nested variable references can also contain modified references and
  61. function invocations (*note Functions for Transforming Text:
  62. Functions.), just like any other reference.  For example, using the
  63. `subst' function (*note Functions for String Substitution and Analysis:
  64. Text Functions.):
  65.      x = variable1
  66.      variable2 := Hello
  67.      y = $(subst 1,2,$(x))
  68.      z = y
  69.      a := $($($(z)))
  70. eventually defines `a' as `Hello'.  It is doubtful that anyone would
  71. ever want to write a nested reference as convoluted as this one, but it
  72. works: `$($($(z)))' expands to `$($(y))' which becomes `$($(subst
  73. 1,2,$(x)))'.  This gets the value `variable1' from `x' and changes it
  74. by substitution to `variable2', so that the entire string becomes
  75. `$(variable2)', a simple variable reference whose value is `Hello'.
  76.    A computed variable name need not consist entirely of a single
  77. variable reference.  It can contain several variable references, as
  78. well as some invariant text.  For example,
  79.      a_dirs := dira dirb
  80.      1_dirs := dir1 dir2
  81.      
  82.      a_files := filea fileb
  83.      1_files := file1 file2
  84.      
  85.      ifeq "$(use_a)" "yes"
  86.      a1 := a
  87.      else
  88.      a1 := 1
  89.      endif
  90.      
  91.      ifeq "$(use_dirs)" "yes"
  92.      df := dirs
  93.      else
  94.      df := files
  95.      endif
  96.      
  97.      dirs := $($(a1)_$(df))
  98. will give `dirs' the same value as `a_dirs', `1_dirs', `a_files' or
  99. `1_files' depending on the settings of `use_a' and `use_dirs'.
  100.    Computed variable names can also be used in substitution references:
  101.      a_objects := a.o b.o c.o
  102.      1_objects := 1.o 2.o 3.o
  103.      
  104.      sources := $($(a1)_objects:.o=.c)
  105. defines `sources' as either `a.c b.c c.c' or `1.c 2.c 3.c', depending
  106. on the value of `a1'.
  107.    The only restriction on this sort of use of nested variable
  108. references is that they cannot specify part of the name of a function
  109. to be called.  This is because the test for a recognized function name
  110. is done before the expansion of nested references.  For example,
  111.      ifdef do_sort
  112.      func := sort
  113.      else
  114.      func := strip
  115.      endif
  116.      
  117.      bar := a d b g q c
  118.      
  119.      foo := $($(func) $(bar))
  120. attempts to give `foo' the value of the variable `sort a d b g q c' or
  121. `strip a d b g q c', rather than giving `a d b g q c' as the argument
  122. to either the `sort' or the `strip' function.  This restriction could
  123. be removed in the future if that change is shown to be a good idea.
  124.    You can also use computed variable names in the left-hand side of a
  125. variable assignment, or in a `define' directive, as in:
  126.      dir = foo
  127.      $(dir)_sources := $(wildcard $(dir)/*.c)
  128.      define $(dir)_print
  129.      lpr $($(dir)_sources)
  130.      endef
  131. This example defines the variables `dir', `foo_sources', and
  132. `foo_print'.
  133.    Note that "nested variable references" are quite different from
  134. "recursively expanded variables" (*note The Two Flavors of Variables:
  135. Flavors.), though both are used together in complex ways when doing
  136. makefile programming.
  137. File: make.info,  Node: Values,  Next: Setting,  Prev: Advanced,  Up: Using Variables
  138. How Variables Get Their Values
  139. ==============================
  140.    Variables can get values in several different ways:
  141.    * You can specify an overriding value when you run `make'.  *Note
  142.      Overriding Variables: Overriding.
  143.    * You can specify a value in the makefile, either with an assignment
  144.      (*note Setting Variables: Setting.) or with a verbatim definition
  145.      (*note Defining Variables Verbatim: Defining.).
  146.    * Variables in the environment become `make' variables.  *Note
  147.      Variables from the Environment: Environment.
  148.    * Several "automatic" variables are given new values for each rule.
  149.      Each of these has a single conventional use.  *Note Automatic
  150.      Variables: Automatic.
  151.    * Several variables have constant initial values.  *Note Variables
  152.      Used by Implicit Rules: Implicit Variables.
  153. File: make.info,  Node: Setting,  Next: Appending,  Prev: Values,  Up: Using Variables
  154. Setting Variables
  155. =================
  156.    To set a variable from the makefile, write a line starting with the
  157. variable name followed by `=' or `:='.  Whatever follows the `=' or
  158. `:=' on the line becomes the value.  For example,
  159.      objects = main.o foo.o bar.o utils.o
  160. defines a variable named `objects'.  Whitespace around the variable
  161. name and immediately after the `=' is ignored.
  162.    Variables defined with `=' are "recursively expanded" variables.
  163. Variables defined with `:=' are "simply expanded" variables; these
  164. definitions can contain variable references which will be expanded
  165. before the definition is made.  *Note The Two Flavors of Variables:
  166. Flavors.
  167.    The variable name may contain function and variable references, which
  168. are expanded when the line is read to find the actual variable name to
  169.    There is no limit on the length of the value of a variable except the
  170. amount of swapping space on the computer.  When a variable definition is
  171. long, it is a good idea to break it into several lines by inserting
  172. backslash-newline at convenient places in the definition.  This will not
  173. affect the functioning of `make', but it will make the makefile easier
  174. to read.
  175.    Most variable names are considered to have the empty string as a
  176. value if you have never set them.  Several variables have built-in
  177. initial values that are not empty, but you can set them in the usual
  178. ways (*note Variables Used by Implicit Rules: Implicit Variables.).
  179. Several special variables are set automatically to a new value for each
  180. rule; these are called the "automatic" variables (*note Automatic
  181. Variables: Automatic.).
  182. File: make.info,  Node: Appending,  Next: Override Directive,  Prev: Setting,  Up: Using Variables
  183. Appending More Text to Variables
  184. ================================
  185.    Often it is useful to add more text to the value of a variable
  186. already defined.  You do this with a line containing `+=', like this:
  187.      objects += another.o
  188. This takes the value of the variable `objects', and adds the text
  189. `another.o' to it (preceded by a single space).  Thus:
  190.      objects = main.o foo.o bar.o utils.o
  191.      objects += another.o
  192. sets `objects' to `main.o foo.o bar.o utils.o another.o'.
  193.    Using `+=' is similar to:
  194.      objects = main.o foo.o bar.o utils.o
  195.      objects := $(objects) another.o
  196. but differs in ways that become important when you use more complex
  197. values.
  198.    When the variable in question has not been defined before, `+=' acts
  199. just like normal `=': it defines a recursively-expanded variable.
  200. However, when there *is* a previous definition, exactly what `+=' does
  201. depends on what flavor of variable you defined originally.  *Note The
  202. Two Flavors of Variables: Flavors, for an explanation of the two
  203. flavors of variables.
  204.    When you add to a variable's value with `+=', `make' acts
  205. essentially as if you had included the extra text in the initial
  206. definition of the variable.  If you defined it first with `:=', making
  207. it a simply-expanded variable, `+=' adds to that simply-expanded
  208. definition, and expands the new text before appending it to the old
  209. value just as `:=' does (*note Setting Variables: Setting., for a full
  210. explanation of `:=').  In fact,
  211.      variable := value
  212.      variable += more
  213. is exactly equivalent to:
  214.      variable := value
  215.      variable := $(variable) more
  216.    On the other hand, when you use `+=' with a variable that you defined
  217. first to be recursively-expanded using plain `=', `make' does something
  218. a bit different.  Recall that when you define a recursively-expanded
  219. variable, `make' does not expand the value you set for variable and
  220. function references immediately.  Instead it stores the text verbatim,
  221. and saves these variable and function references to be expanded later,
  222. when you refer to the new variable (*note The Two Flavors of Variables:
  223. Flavors.).  When you use `+=' on a recursively-expanded variable, it is
  224. this unexpanded text to which `make' appends the new text you specify.
  225.      variable = value
  226.      variable += more
  227. is roughly equivalent to:
  228.      temp = value
  229.      variable = $(temp) more
  230. except that of course it never defines a variable called `temp'.  The
  231. importance of this comes when the variable's old value contains
  232. variable references.  Take this common example:
  233.      CFLAGS = $(includes) -O
  234.      ...
  235.      CFLAGS += -pg # enable profiling
  236. The first line defines the `CFLAGS' variable with a reference to another
  237. variable, `includes'.  (`CFLAGS' is used by the rules for C
  238. compilation; *note Catalogue of Implicit Rules: Catalogue of Rules..)
  239. Using `=' for the definition makes `CFLAGS' a recursively-expanded
  240. variable, meaning `$(includes) -O' is *not* expanded when `make'
  241. processes the definition of `CFLAGS'.  Thus, `includes' need not be
  242. defined yet for its value to take effect.  It only has to be defined
  243. before any reference to `CFLAGS'.  If we tried to append to the value
  244. of `CFLAGS' without using `+=', we might do it like this:
  245.      CFLAGS := $(CFLAGS) -pg # enable profiling
  246. This is close, but not quite what we want.  Using `:=' redefines
  247. `CFLAGS' as a simply-expanded variable; this means `make' expands the
  248. text `$(CFLAGS) -pg' before setting the variable.  If `includes' is not
  249. yet defined, we get ` -O -pg', and a later definition of `includes'
  250. will have no effect.  Conversely, by using `+=' we set `CFLAGS' to the
  251. *unexpanded* value `$(includes) -O -pg'.  Thus we preserve the
  252. reference to `includes', so if that variable gets defined at any later
  253. point, a reference like `$(CFLAGS)' still uses its value.
  254. File: make.info,  Node: Override Directive,  Next: Defining,  Prev: Appending,  Up: Using Variables
  255. The `override' Directive
  256. ========================
  257.    If a variable has been set with a command argument (*note Overriding
  258. Variables: Overriding.), then ordinary assignments in the makefile are
  259. ignored.  If you want to set the variable in the makefile even though
  260. it was set with a command argument, you can use an `override'
  261. directive, which is a line that looks like this:
  262.      override VARIABLE = VALUE
  263.      override VARIABLE := VALUE
  264.    To append more text to a variable defined on the command line, use:
  265.      override VARIABLE += MORE TEXT
  266. *Note Appending More Text to Variables: Appending.
  267.    The `override' directive was not invented for escalation in the war
  268. between makefiles and command arguments.  It was invented so you can
  269. alter and add to values that the user specifies with command arguments.
  270.    For example, suppose you always want the `-g' switch when you run the
  271. C compiler, but you would like to allow the user to specify the other
  272. switches with a command argument just as usual.  You could use this
  273. `override' directive:
  274.      override CFLAGS += -g
  275.    You can also use `override' directives with `define' directives.
  276. This is done as you might expect:
  277.      override define foo
  278.      bar
  279.      endef
  280. *Note Defining Variables Verbatim: Defining.
  281. File: make.info,  Node: Defining,  Next: Environment,  Prev: Override Directive,  Up: Using Variables
  282. Defining Variables Verbatim
  283. ===========================
  284. Another way to set the value of a variable is to use the `define'
  285. directive.  This directive has an unusual syntax which allows newline
  286. characters to be included in the value, which is convenient for defining
  287. canned sequences of commands (*note Defining Canned Command Sequences:
  288. Sequences.).
  289.    The `define' directive is followed on the same line by the name of
  290. the variable and nothing more.  The value to give the variable appears
  291. on the following lines.  The end of the value is marked by a line
  292. containing just the word `endef'.  Aside from this difference in
  293. syntax, `define' works just like `=': it creates a recursively-expanded
  294. variable (*note The Two Flavors of Variables: Flavors.).  The variable
  295. name may contain function and variable references, which are expanded
  296. when the directive is read to find the actual variable name to use.
  297.      define two-lines
  298.      echo foo
  299.      echo $(bar)
  300.      endef
  301.    The value in an ordinary assignment cannot contain a newline; but the
  302. newlines that separate the lines of the value in a `define' become part
  303. of the variable's value (except for the final newline which precedes
  304. the `endef' and is not considered part of the value).
  305.    The previous example is functionally equivalent to this:
  306.      two-lines = echo foo; echo $(bar)
  307. since two commands separated by semicolon behave much like two separate
  308. shell commands.  However, note that using two separate lines means
  309. `make' will invoke the shell twice, running an independent subshell for
  310. each line.  *Note Command Execution: Execution.
  311.    If you want variable definitions made with `define' to take
  312. precedence over command-line variable definitions, you can use the
  313. `override' directive together with `define':
  314.      override define two-lines
  315.      foo
  316.      $(bar)
  317.      endef
  318. *Note The `override' Directive: Override Directive.
  319. File: make.info,  Node: Environment,  Prev: Defining,  Up: Using Variables
  320. Variables from the Environment
  321. ==============================
  322.    Variables in `make' can come from the environment in which `make' is
  323. run.  Every environment variable that `make' sees when it starts up is
  324. transformed into a `make' variable with the same name and value.  But
  325. an explicit assignment in the makefile, or with a command argument,
  326. overrides the environment.  (If the `-e' flag is specified, then values
  327. from the environment override assignments in the makefile.  *Note
  328. Summary of Options: Options Summary.  But this is not recommended
  329. practice.)
  330.    Thus, by setting the variable `CFLAGS' in your environment, you can
  331. cause all C compilations in most makefiles to use the compiler switches
  332. you prefer.  This is safe for variables with standard or conventional
  333. meanings because you know that no makefile will use them for other
  334. things.  (But this is not totally reliable; some makefiles set `CFLAGS'
  335. explicitly and therefore are not affected by the value in the
  336. environment.)
  337.    When `make' is invoked recursively, variables defined in the outer
  338. invocation can be passed to inner invocations through the environment
  339. (*note Recursive Use of `make': Recursion.).  By default, only
  340. variables that came from the environment or the command line are passed
  341. to recursive invocations.  You can use the `export' directive to pass
  342. other variables.  *Note Communicating Variables to a Sub-`make':
  343. Variables/Recursion, for full details.
  344.    Other use of variables from the environment is not recommended.  It
  345. is not wise for makefiles to depend for their functioning on
  346. environment variables set up outside their control, since this would
  347. cause different users to get different results from the same makefile.
  348. This is against the whole purpose of most makefiles.
  349.    Such problems would be especially likely with the variable `SHELL',
  350. which is normally present in the environment to specify the user's
  351. choice of interactive shell.  It would be very undesirable for this
  352. choice to affect `make'.  So `make' ignores the environment value of
  353. `SHELL'.
  354. File: make.info,  Node: Conditionals,  Next: Functions,  Prev: Using Variables,  Up: Top
  355. Conditional Parts of Makefiles
  356. ******************************
  357.    A "conditional" causes part of a makefile to be obeyed or ignored
  358. depending on the values of variables.  Conditionals can compare the
  359. value of one variable to another, or the value of a variable to a
  360. constant string.  Conditionals control what `make' actually "sees" in
  361. the makefile, so they *cannot* be used to control shell commands at the
  362. time of execution.
  363. * Menu:
  364. * Conditional Example::         Example of a conditional
  365. * Conditional Syntax::          The syntax of conditionals.
  366. * Testing Flags::               Conditionals that test flags.
  367. File: make.info,  Node: Conditional Example,  Next: Conditional Syntax,  Up: Conditionals
  368. Example of a Conditional
  369. ========================
  370.    The following example of a conditional tells `make' to use one set
  371. of libraries if the `CC' variable is `gcc', and a different set of
  372. libraries otherwise.  It works by controlling which of two command
  373. lines will be used as the command for a rule.  The result is that
  374. `CC=gcc' as an argument to `make' changes not only which compiler is
  375. used but also which libraries are linked.
  376.      libs_for_gcc = -lgnu
  377.      normal_libs =
  378.      
  379.      foo: $(objects)
  380.      ifeq ($(CC),gcc)
  381.              $(CC) -o foo $(objects) $(libs_for_gcc)
  382.      else
  383.              $(CC) -o foo $(objects) $(normal_libs)
  384.      endif
  385.    This conditional uses three directives: one `ifeq', one `else' and
  386. one `endif'.
  387.    The `ifeq' directive begins the conditional, and specifies the
  388. condition.  It contains two arguments, separated by a comma and
  389. surrounded by parentheses.  Variable substitution is performed on both
  390. arguments and then they are compared.  The lines of the makefile
  391. following the `ifeq' are obeyed if the two arguments match; otherwise
  392. they are ignored.
  393.    The `else' directive causes the following lines to be obeyed if the
  394. previous conditional failed.  In the example above, this means that the
  395. second alternative linking command is used whenever the first
  396. alternative is not used.  It is optional to have an `else' in a
  397. conditional.
  398.    The `endif' directive ends the conditional.  Every conditional must
  399. end with an `endif'.  Unconditional makefile text follows.
  400.    As this example illustrates, conditionals work at the textual level:
  401. the lines of the conditional are treated as part of the makefile, or
  402. ignored, according to the condition.  This is why the larger syntactic
  403. units of the makefile, such as rules, may cross the beginning or the
  404. end of the conditional.
  405.    When the variable `CC' has the value `gcc', the above example has
  406. this effect:
  407.      foo: $(objects)
  408.              $(CC) -o foo $(objects) $(libs_for_gcc)
  409. When the variable `CC' has any other value, the effect is this:
  410.      foo: $(objects)
  411.              $(CC) -o foo $(objects) $(normal_libs)
  412.    Equivalent results can be obtained in another way by
  413. conditionalizing a variable assignment and then using the variable
  414. unconditionally:
  415.      libs_for_gcc = -lgnu
  416.      normal_libs =
  417.      
  418.      ifeq ($(CC),gcc)
  419.        libs=$(libs_for_gcc)
  420.      else
  421.        libs=$(normal_libs)
  422.      endif
  423.      
  424.      foo: $(objects)
  425.              $(CC) -o foo $(objects) $(libs)
  426. File: make.info,  Node: Conditional Syntax,  Next: Testing Flags,  Prev: Conditional Example,  Up: Conditionals
  427. Syntax of Conditionals
  428. ======================
  429.    The syntax of a simple conditional with no `else' is as follows:
  430.      CONDITIONAL-DIRECTIVE
  431.      TEXT-IF-TRUE
  432.      endif
  433. The TEXT-IF-TRUE may be any lines of text, to be considered as part of
  434. the makefile if the condition is true.  If the condition is false, no
  435. text is used instead.
  436.    The syntax of a complex conditional is as follows:
  437.      CONDITIONAL-DIRECTIVE
  438.      TEXT-IF-TRUE
  439.      else
  440.      TEXT-IF-FALSE
  441.      endif
  442. If the condition is true, TEXT-IF-TRUE is used; otherwise,
  443. TEXT-IF-FALSE is used instead.  The TEXT-IF-FALSE can be any number of
  444. lines of text.
  445.    The syntax of the CONDITIONAL-DIRECTIVE is the same whether the
  446. conditional is simple or complex.  There are four different directives
  447. that test different conditions.  Here is a table of them:
  448. `ifeq (ARG1, ARG2)'
  449. `ifeq 'ARG1' 'ARG2''
  450. `ifeq "ARG1" "ARG2"'
  451. `ifeq "ARG1" 'ARG2''
  452. `ifeq 'ARG1' "ARG2"'
  453.      Expand all variable references in ARG1 and ARG2 and compare them.
  454.      If they are identical, the TEXT-IF-TRUE is effective; otherwise,
  455.      the TEXT-IF-FALSE, if any, is effective.
  456.      Often you want to test if a variable has a non-empty value.  When
  457.      the value results from complex expansions of variables and
  458.      functions, expansions you would consider empty may actually
  459.      contain whitespace characters and thus are not seen as empty.
  460.      However, you can use the `strip' function (*note Text
  461.      Functions::.) to avoid interpreting whitespace as a non-empty
  462.      value.  For example:
  463.           ifeq ($(strip $(foo)),)
  464.           TEXT-IF-EMPTY
  465.           endif
  466.      will evaluate TEXT-IF-EMPTY even if the expansion of `$(foo)'
  467.      contains whitespace characters.
  468. `ifneq (ARG1, ARG2)'
  469. `ifneq 'ARG1' 'ARG2''
  470. `ifneq "ARG1" "ARG2"'
  471. `ifneq "ARG1" 'ARG2''
  472. `ifneq 'ARG1' "ARG2"'
  473.      Expand all variable references in ARG1 and ARG2 and compare them.
  474.      If they are different, the TEXT-IF-TRUE is effective; otherwise,
  475.      the TEXT-IF-FALSE, if any, is effective.
  476. `ifdef VARIABLE-NAME'
  477.      If the variable VARIABLE-NAME has a non-empty value, the
  478.      TEXT-IF-TRUE is effective; otherwise, the TEXT-IF-FALSE, if any,
  479.      is effective.  Variables that have never been defined have an
  480.      empty value.
  481.      Note that `ifdef' only tests whether a variable has a value.  It
  482.      does not expand the variable to see if that value is nonempty.
  483.      Consequently, tests using `ifdef' return true for all definitions
  484.      except those like `foo ='.  To test for an empty value, use
  485.      `ifeq ($(foo),)'.  For example,
  486.           bar =
  487.           foo = $(bar)
  488.           ifdef foo
  489.           frobozz = yes
  490.           else
  491.           frobozz = no
  492.           endif
  493.      sets `frobozz' to `yes', while:
  494.           foo =
  495.           ifdef foo
  496.           frobozz = yes
  497.           else
  498.           frobozz = no
  499.           endif
  500.      sets `frobozz' to `no'.
  501. `ifndef VARIABLE-NAME'
  502.      If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE
  503.      is effective; otherwise, the TEXT-IF-FALSE, if any, is effective.
  504.    Extra spaces are allowed and ignored at the beginning of the
  505. conditional directive line, but a tab is not allowed.  (If the line
  506. begins with a tab, it will be considered a command for a rule.)  Aside
  507. from this, extra spaces or tabs may be inserted with no effect anywhere
  508. except within the directive name or within an argument.  A comment
  509. starting with `#' may appear at the end of the line.
  510.    The other two directives that play a part in a conditional are `else'
  511. and `endif'.  Each of these directives is written as one word, with no
  512. arguments.  Extra spaces are allowed and ignored at the beginning of the
  513. line, and spaces or tabs at the end.  A comment starting with `#' may
  514. appear at the end of the line.
  515.    Conditionals affect which lines of the makefile `make' uses.  If the
  516. condition is true, `make' reads the lines of the TEXT-IF-TRUE as part
  517. of the makefile; if the condition is false, `make' ignores those lines
  518. completely.  It follows that syntactic units of the makefile, such as
  519. rules, may safely be split across the beginning or the end of the
  520. conditional.
  521.    `make' evaluates conditionals when it reads a makefile.
  522. Consequently, you cannot use automatic variables in the tests of
  523. conditionals because they are not defined until commands are run (*note
  524. Automatic Variables: Automatic.).
  525.    To prevent intolerable confusion, it is not permitted to start a
  526. conditional in one makefile and end it in another.  However, you may
  527. write an `include' directive within a conditional, provided you do not
  528. attempt to terminate the conditional inside the included file.
  529. File: make.info,  Node: Testing Flags,  Prev: Conditional Syntax,  Up: Conditionals
  530. Conditionals that Test Flags
  531. ============================
  532.    You can write a conditional that tests `make' command flags such as
  533. `-t' by using the variable `MAKEFLAGS' together with the `findstring'
  534. function (*note Functions for String Substitution and Analysis: Text
  535. Functions.).  This is useful when `touch' is not enough to make a file
  536. appear up to date.
  537.    The `findstring' function determines whether one string appears as a
  538. substring of another.  If you want to test for the `-t' flag, use `t'
  539. as the first string and the value of `MAKEFLAGS' as the other.
  540.    For example, here is how to arrange to use `ranlib -t' to finish
  541. marking an archive file up to date:
  542.      archive.a: ...
  543.      ifneq (,$(findstring t,$(MAKEFLAGS)))
  544.              +touch archive.a
  545.              +ranlib -t archive.a
  546.      else
  547.              ranlib archive.a
  548.      endif
  549. The `+' prefix marks those command lines as "recursive" so that they
  550. will be executed despite use of the `-t' flag.  *Note Recursive Use of
  551. `make': Recursion.
  552. File: make.info,  Node: Functions,  Next: Running,  Prev: Conditionals,  Up: Top
  553. Functions for Transforming Text
  554. *******************************
  555.    "Functions" allow you to do text processing in the makefile to
  556. compute the files to operate on or the commands to use.  You use a
  557. function in a "function call", where you give the name of the function
  558. and some text (the "arguments") for the function to operate on.  The
  559. result of the function's processing is substituted into the makefile at
  560. the point of the call, just as a variable might be substituted.
  561. * Menu:
  562. * Syntax of Functions::         How to write a function call.
  563. * Text Functions::              General-purpose text manipulation functions.
  564. * Filename Functions::          Functions for manipulating file names.
  565. * Foreach Function::            Repeat some text with controlled variation.
  566. * Origin Function::             Find where a variable got its value.
  567. * Shell Function::              Substitute the output of a shell command.
  568. File: make.info,  Node: Syntax of Functions,  Next: Text Functions,  Up: Functions
  569. Function Call Syntax
  570. ====================
  571.    A function call resembles a variable reference.  It looks like this:
  572.      $(FUNCTION ARGUMENTS)
  573. or like this:
  574.      ${FUNCTION ARGUMENTS}
  575.    Here FUNCTION is a function name; one of a short list of names that
  576. are part of `make'.  There is no provision for defining new functions.
  577.    The ARGUMENTS are the arguments of the function.  They are separated
  578. from the function name by one or more spaces or tabs, and if there is
  579. more than one argument, then they are separated by commas.  Such
  580. whitespace and commas are not part of an argument's value.  The
  581. delimiters which you use to surround the function call, whether
  582. parentheses or braces, can appear in an argument only in matching pairs;
  583. the other kind of delimiters may appear singly.  If the arguments
  584. themselves contain other function calls or variable references, it is
  585. wisest to use the same kind of delimiters for all the references; write
  586. `$(subst a,b,$(x))', not `$(subst a,b,${x})'.  This is because it is
  587. clearer, and because only one type of delimiter is matched to find the
  588. end of the reference.
  589.    The text written for each argument is processed by substitution of
  590. variables and function calls to produce the argument value, which is
  591. the text on which the function acts.  The substitution is done in the
  592. order in which the arguments appear.
  593.    Commas and unmatched parentheses or braces cannot appear in the text
  594. of an argument as written; leading spaces cannot appear in the text of
  595. the first argument as written.  These characters can be put into the
  596. argument value by variable substitution.  First define variables
  597. `comma' and `space' whose values are isolated comma and space
  598. characters, then substitute these variables where such characters are
  599. wanted, like this:
  600.      comma:= ,
  601.      empty:=
  602.      space:= $(empty) $(empty)
  603.      foo:= a b c
  604.      bar:= $(subst $(space),$(comma),$(foo))
  605.      # bar is now `a,b,c'.
  606. Here the `subst' function replaces each space with a comma, through the
  607. value of `foo', and substitutes the result.
  608. File: make.info,  Node: Text Functions,  Next: Filename Functions,  Prev: Syntax of Functions,  Up: Functions
  609. Functions for String Substitution and Analysis
  610. ==============================================
  611.    Here are some functions that operate on strings:
  612. `$(subst FROM,TO,TEXT)'
  613.      Performs a textual replacement on the text TEXT: each occurrence
  614.      of FROM is replaced by TO.  The result is substituted for the
  615.      function call.  For example,
  616.           $(subst ee,EE,feet on the street)
  617.      substitutes the string `fEEt on the strEEt'.
  618. `$(patsubst PATTERN,REPLACEMENT,TEXT)'
  619.      Finds whitespace-separated words in TEXT that match PATTERN and
  620.      replaces them with REPLACEMENT.  Here PATTERN may contain a `%'
  621.      which acts as a wildcard, matching any number of any characters
  622.      within a word.  If REPLACEMENT also contains a `%', the `%' is
  623.      replaced by the text that matched the `%' in PATTERN.
  624.      `%' characters in `patsubst' function invocations can be quoted
  625.      with preceding backslashes (`\').  Backslashes that would
  626.      otherwise quote `%' characters can be quoted with more backslashes.
  627.      Backslashes that quote `%' characters or other backslashes are
  628.      removed from the pattern before it is compared file names or has a
  629.      stem substituted into it.  Backslashes that are not in danger of
  630.      quoting `%' characters go unmolested.  For example, the pattern
  631.      `the\%weird\\%pattern\\' has `the%weird\' preceding the operative
  632.      `%' character, and `pattern\\' following it.  The final two
  633.      backslashes are left alone because they cannot affect any `%'
  634.      character.
  635.      Whitespace between words is folded into single space characters;
  636.      leading and trailing whitespace is discarded.
  637.      For example,
  638.           $(patsubst %.c,%.o,x.c.c bar.c)
  639.      produces the value `x.c.o bar.o'.
  640.      Substitution references (*note Substitution References:
  641.      Substitution Refs.) are a simpler way to get the effect of the
  642.      `patsubst' function:
  643.           $(VAR:PATTERN=REPLACEMENT)
  644.      is equivalent to
  645.           $(patsubst PATTERN,REPLACEMENT,$(VAR))
  646.      The second shorthand simplifies one of the most common uses of
  647.      `patsubst': replacing the suffix at the end of file names.
  648.           $(VAR:SUFFIX=REPLACEMENT)
  649.      is equivalent to
  650.           $(patsubst %SUFFIX,%REPLACEMENT,$(VAR))
  651.      For example, you might have a list of object files:
  652.           objects = foo.o bar.o baz.o
  653.      To get the list of corresponding source files, you could simply
  654.      write:
  655.           $(objects:.o=.c)
  656.      instead of using the general form:
  657.           $(patsubst %.o,%.c,$(objects))
  658. `$(strip STRING)'
  659.      Removes leading and trailing whitespace from STRING and replaces
  660.      each internal sequence of one or more whitespace characters with a
  661.      single space.  Thus, `$(strip a b  c )' results in `a b c'.
  662.      The function `strip' can be very useful when used in conjunction
  663.      with conditionals.  When comparing something with the null string
  664.      `""' using `ifeq' or `ifneq', you usually want a string of just
  665.      whitespace to match the null string (*note Conditionals::.).
  666.      Thus, the following may fail to have the desired results:
  667.           .PHONY: all
  668.           ifneq   "$(needs_made)" ""
  669.           all: $(needs_made)
  670.           else
  671.           all:;@echo 'Nothing to make!'
  672.           endif
  673.      Replacing the variable reference `$(needs_made)' with the function
  674.      call `$(strip $(needs_made))' in the `ifneq' directive would make
  675.      it more robust.
  676. `$(findstring FIND,IN)'
  677.      Searches IN for an occurrence of FIND.  If it occurs, the value is
  678.      FIND; otherwise, the value is empty.  You can use this function in
  679.      a conditional to test for the presence of a specific substring in
  680.      a given string.  Thus, the two examples,
  681.           $(findstring a,a b c)
  682.           $(findstring a,b c)
  683.      produce the values `a' and `' (the empty string), respectively.
  684.      *Note Testing Flags::, for a practical application of `findstring'.
  685. `$(filter PATTERN...,TEXT)'
  686.      Removes all whitespace-separated words in TEXT that do *not* match
  687.      any of the PATTERN words, returning only matching words.  The
  688.      patterns are written using `%', just like the patterns used in the
  689.      `patsubst' function above.
  690.      The `filter' function can be used to separate out different types
  691.      of strings (such as file names) in a variable.  For example:
  692.           sources := foo.c bar.c baz.s ugh.h
  693.           foo: $(sources)
  694.                   cc $(filter %.c %.s,$(sources)) -o foo
  695.      says that `foo' depends of `foo.c', `bar.c', `baz.s' and `ugh.h'
  696.      but only `foo.c', `bar.c' and `baz.s' should be specified in the
  697.      command to the compiler.
  698. `$(filter-out PATTERN...,TEXT)'
  699.      Removes all whitespace-separated words in TEXT that *do* match the
  700.      PATTERN words, returning only the words that *do not* match.  This
  701.      is the exact opposite of the `filter' function.
  702.      For example, given:
  703.           objects=main1.o foo.o main2.o bar.o
  704.           mains=main1.o main2.o
  705.      the following generates a list which contains all the object files
  706.      not in `mains':
  707.           $(filter-out $(mains),$(objects))
  708. `$(sort LIST)'
  709.      Sorts the words of LIST in lexical order, removing duplicate
  710.      words.  The output is a list of words separated by single spaces.
  711.      Thus,
  712.           $(sort foo bar lose)
  713.      returns the value `bar foo lose'.
  714.      Incidentally, since `sort' removes duplicate words, you can use it
  715.      for this purpose even if you don't care about the sort order.
  716.    Here is a realistic example of the use of `subst' and `patsubst'.
  717. Suppose that a makefile uses the `VPATH' variable to specify a list of
  718. directories that `make' should search for dependency files (*note
  719. `VPATH' Search Path for All Dependencies: General Search.).  This
  720. example shows how to tell the C compiler to search for header files in
  721. the same list of directories.
  722.    The value of `VPATH' is a list of directories separated by colons,
  723. such as `src:../headers'.  First, the `subst' function is used to
  724. change the colons to spaces:
  725.      $(subst :, ,$(VPATH))
  726. This produces `src ../headers'.  Then `patsubst' is used to turn each
  727. directory name into a `-I' flag.  These can be added to the value of
  728. the variable `CFLAGS', which is passed automatically to the C compiler,
  729. like this:
  730.      override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
  731. The effect is to append the text `-Isrc -I../headers' to the previously
  732. given value of `CFLAGS'.  The `override' directive is used so that the
  733. new value is assigned even if the previous value of `CFLAGS' was
  734. specified with a command argument (*note The `override' Directive:
  735. Override Directive.).
  736. File: make.info,  Node: Filename Functions,  Next: Foreach Function,  Prev: Text Functions,  Up: Functions
  737. Functions for File Names
  738. ========================
  739.    Several of the built-in expansion functions relate specifically to
  740. taking apart file names or lists of file names.
  741.    Each of the following functions performs a specific transformation
  742. on a file name.  The argument of the function is regarded as a series
  743. of file names, separated by whitespace.  (Leading and trailing
  744. whitespace is ignored.)  Each file name in the series is transformed in
  745. the same way and the results are concatenated with single spaces
  746. between them.
  747. `$(dir NAMES...)'
  748.      Extracts the directory-part of each file name in NAMES.  The
  749.      directory-part of the file name is everything up through (and
  750.      including) the last slash in it.  If the file name contains no
  751.      slash, the directory part is the string `./'.  For example,
  752.           $(dir src/foo.c hacks)
  753.      produces the result `src/ ./'.
  754. `$(notdir NAMES...)'
  755.      Extracts all but the directory-part of each file name in NAMES.
  756.      If the file name contains no slash, it is left unchanged.
  757.      Otherwise, everything through the last slash is removed from it.
  758.      A file name that ends with a slash becomes an empty string.  This
  759.      is unfortunate, because it means that the result does not always
  760.      have the same number of whitespace-separated file names as the
  761.      argument had; but we do not see any other valid alternative.
  762.      For example,
  763.           $(notdir src/foo.c hacks)
  764.      produces the result `foo.c hacks'.
  765. `$(suffix NAMES...)'
  766.      Extracts the suffix of each file name in NAMES.  If the file name
  767.      contains a period, the suffix is everything starting with the last
  768.      period.  Otherwise, the suffix is the empty string.  This
  769.      frequently means that the result will be empty when NAMES is not,
  770.      and if NAMES contains multiple file names, the result may contain
  771.      fewer file names.
  772.      For example,
  773.           $(suffix src/foo.c hacks)
  774.      produces the result `.c'.
  775. `$(basename NAMES...)'
  776.      Extracts all but the suffix of each file name in NAMES.  If the
  777.      file name contains a period, the basename is everything starting
  778.      up to (and not including) the last period.  Otherwise, the
  779.      basename is the entire file name.  For example,
  780.           $(basename src/foo.c hacks)
  781.      produces the result `src/foo hacks'.
  782. `$(addsuffix SUFFIX,NAMES...)'
  783.      The argument NAMES is regarded as a series of names, separated by
  784.      whitespace; SUFFIX is used as a unit.  The value of SUFFIX is
  785.      appended to the end of each individual name and the resulting
  786.      larger names are concatenated with single spaces between them.
  787.      For example,
  788.           $(addsuffix .c,foo bar)
  789.      produces the result `foo.c bar.c'.
  790. `$(addprefix PREFIX,NAMES...)'
  791.      The argument NAMES is regarded as a series of names, separated by
  792.      whitespace; PREFIX is used as a unit.  The value of PREFIX is
  793.      prepended to the front of each individual name and the resulting
  794.      larger names are concatenated with single spaces between them.
  795.      For example,
  796.           $(addprefix src/,foo bar)
  797.      produces the result `src/foo src/bar'.
  798. `$(join LIST1,LIST2)'
  799.      Concatenates the two arguments word by word: the two first words
  800.      (one from each argument) concatenated form the first word of the
  801.      result, the two second words form the second word of the result,
  802.      and so on.  So the Nth word of the result comes from the Nth word
  803.      of each argument.  If one argument has more words that the other,
  804.      the extra words are copied unchanged into the result.
  805.      For example, `$(join a b,.c .o)' produces `a.c b.o'.
  806.      Whitespace between the words in the lists is not preserved; it is
  807.      replaced with a single space.
  808.      This function can merge the results of the `dir' and `notdir'
  809.      functions, to produce the original list of files which was given
  810.      to those two functions.
  811. `$(word N,TEXT)'
  812.      Returns the Nth word of TEXT.  The legitimate values of N start
  813.      from 1.  If N is bigger than the number of words in TEXT, the
  814.      value is empty.  For example,
  815.           $(word 2, foo bar baz)
  816.      returns `bar'.
  817. `$(words TEXT)'
  818.      Returns the number of words in TEXT.  Thus, the last word of TEXT
  819.      is `$(word $(words TEXT),TEXT)'.
  820. `$(firstword NAMES...)'
  821.      The argument NAMES is regarded as a series of names, separated by
  822.      whitespace.  The value is the first name in the series.  The rest
  823.      of the names are ignored.
  824.      For example,
  825.           $(firstword foo bar)
  826.      produces the result `foo'.  Although `$(firstword TEXT)' is the
  827.      same as `$(word 1,TEXT)', the `firstword' function is retained for
  828.      its simplicity.
  829. `$(wildcard PATTERN)'
  830.      The argument PATTERN is a file name pattern, typically containing
  831.      wildcard characters (as in shell file name patterns).  The result
  832.      of `wildcard' is a space-separated list of the names of existing
  833.      files that match the pattern.  *Note Using Wildcard Characters in
  834.      File Names: Wildcards.
  835. File: make.info,  Node: Foreach Function,  Next: Origin Function,  Prev: Filename Functions,  Up: Functions
  836. The `foreach' Function
  837. ======================
  838.    The `foreach' function is very different from other functions.  It
  839. causes one piece of text to be used repeatedly, each time with a
  840. different substitution performed on it.  It resembles the `for' command
  841. in the shell `sh' and the `foreach' command in the C-shell `csh'.
  842.    The syntax of the `foreach' function is:
  843.      $(foreach VAR,LIST,TEXT)
  844. The first two arguments, VAR and LIST, are expanded before anything
  845. else is done; note that the last argument, TEXT, is *not* expanded at
  846. the same time.  Then for each word of the expanded value of LIST, the
  847. variable named by the expanded value of VAR is set to that word, and
  848. TEXT is expanded.  Presumably TEXT contains references to that
  849. variable, so its expansion will be different each time.
  850.    The result is that TEXT is expanded as many times as there are
  851. whitespace-separated words in LIST.  The multiple expansions of TEXT
  852. are concatenated, with spaces between them, to make the result of
  853. `foreach'.
  854.    This simple example sets the variable `files' to the list of all
  855. files in the directories in the list `dirs':
  856.      dirs := a b c d
  857.      files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
  858.    Here TEXT is `$(wildcard $(dir)/*)'.  The first repetition finds the
  859. value `a' for `dir', so it produces the same result as `$(wildcard
  860. a/*)'; the second repetition produces the result of `$(wildcard b/*)';
  861. and the third, that of `$(wildcard c/*)'.
  862.    This example has the same result (except for setting `dirs') as the
  863. following example:
  864.      files := $(wildcard a/* b/* c/* d/*)
  865.    When TEXT is complicated, you can improve readability by giving it a
  866. name, with an additional variable:
  867.      find_files = $(wildcard $(dir)/*)
  868.      dirs := a b c d
  869.      files := $(foreach dir,$(dirs),$(find_files))
  870. Here we use the variable `find_files' this way.  We use plain `=' to
  871. define a recursively-expanding variable, so that its value contains an
  872. actual function call to be reexpanded under the control of `foreach'; a
  873. simply-expanded variable would not do, since `wildcard' would be called
  874. only once at the time of defining `find_files'.
  875.    The `foreach' function has no permanent effect on the variable VAR;
  876. its value and flavor after the `foreach' function call are the same as
  877. they were beforehand.  The other values which are taken from LIST are
  878. in effect only temporarily, during the execution of `foreach'.  The
  879. variable VAR is a simply-expanded variable during the execution of
  880. `foreach'.  If VAR was undefined before the `foreach' function call, it
  881. is undefined after the call.  *Note The Two Flavors of Variables:
  882. Flavors.
  883.    You must take care when using complex variable expressions that
  884. result in variable names because many strange things are valid variable
  885. names, but are probably not what you intended.  For example,
  886.      files := $(foreach Es escrito en espanol!,b c ch,$(find_files))
  887. might be useful if the value of `find_files' references the variable
  888. whose name is `Es escrito en espanol!' (es un nombre bastante largo,
  889. no?), but it is more likely to be a mistake.
  890. File: make.info,  Node: Origin Function,  Next: Shell Function,  Prev: Foreach Function,  Up: Functions
  891. The `origin' Function
  892. =====================
  893.    The `origin' function is unlike most other functions in that it does
  894. not operate on the values of variables; it tells you something *about*
  895. a variable.  Specifically, it tells you where it came from.
  896.    The syntax of the `origin' function is:
  897.      $(origin VARIABLE)
  898.    Note that VARIABLE is the *name* of a variable to inquire about; not
  899. a *reference* to that variable.  Therefore you would not normally use a
  900. `$' or parentheses when writing it.  (You can, however, use a variable
  901. reference in the name if you want the name not to be a constant.)
  902.    The result of this function is a string telling you how the variable
  903. VARIABLE was defined:
  904. `undefined'
  905.      if VARIABLE was never defined.
  906. `default'
  907.      if VARIABLE has a default definition, as is usual with `CC' and so
  908.      on.  *Note Variables Used by Implicit Rules: Implicit Variables.
  909.      Note that if you have redefined a default variable, the `origin'
  910.      function will return the origin of the later definition.
  911. `environment'
  912.      if VARIABLE was defined as an environment variable and the `-e'
  913.      option is *not* turned on (*note Summary of Options: Options
  914.      Summary.).
  915. `environment override'
  916.      if VARIABLE was defined as an environment variable and the `-e'
  917.      option *is* turned on (*note Summary of Options: Options Summary.).
  918. `file'
  919.      if VARIABLE was defined in a makefile.
  920. `command line'
  921.      if VARIABLE was defined on the command line.
  922. `override'
  923.      if VARIABLE was defined with an `override' directive in a makefile
  924.      (*note The `override' Directive: Override Directive.).
  925. `automatic'
  926.      if VARIABLE is an automatic variable defined for the execution of
  927.      the commands for each rule (*note Automatic Variables: Automatic.).
  928.    This information is primarily useful (other than for your curiosity)
  929. to determine if you want to believe the value of a variable.  For
  930. example, suppose you have a makefile `foo' that includes another
  931. makefile `bar'.  You want a variable `bletch' to be defined in `bar' if
  932. you run the command `make -f bar', even if the environment contains a
  933. definition of `bletch'.  However, if `foo' defined `bletch' before
  934. including `bar', you do not want to override that definition.  This
  935. could be done by using an `override' directive in `foo', giving that
  936. definition precedence over the later definition in `bar';
  937. unfortunately, the `override' directive would also override any command
  938. line definitions.  So, `bar' could include:
  939.      ifdef bletch
  940.      ifeq "$(origin bletch)" "environment"
  941.      bletch = barf, gag, etc.
  942.      endif
  943.      endif
  944. If `bletch' has been defined from the environment, this will redefine
  945.    If you want to override a previous definition of `bletch' if it came
  946. from the environment, even under `-e', you could instead write:
  947.      ifneq "$(findstring environment,$(origin bletch))" ""
  948.      bletch = barf, gag, etc.
  949.      endif
  950.    Here the redefinition takes place if `$(origin bletch)' returns
  951. either `environment' or `environment override'.  *Note Functions for
  952. String Substitution and Analysis: Text Functions.
  953. File: make.info,  Node: Shell Function,  Prev: Origin Function,  Up: Functions
  954. The `shell' Function
  955. ====================
  956.    The `shell' function is unlike any other function except the
  957. `wildcard' function (*note The Function `wildcard': Wildcard Function.)
  958. in that it communicates with the world outside of `make'.
  959.    The `shell' function performs the same function that backquotes
  960. (``') perform in most shells: it does "command expansion".  This means
  961. that it takes an argument that is a shell command and returns the
  962. output of the command.  The only processing `make' does on the result,
  963. before substituting it into the surrounding text, is to convert
  964. newlines to spaces.
  965.    The commands run by calls to the `shell' function are run when the
  966. function calls are expanded.  In most cases, this is when the makefile
  967. is read in.  The exception is that function calls in the commands of
  968. the rules are expanded when the commands are run, and this applies to
  969. `shell' function calls like all others.
  970.    Here are some examples of the use of the `shell' function:
  971.      contents := $(shell cat foo)
  972. sets `contents' to the contents of the file `foo', with a space (rather
  973. than a newline) separating each line.
  974.      files := $(shell echo *.c)
  975. sets `files' to the expansion of `*.c'.  Unless `make' is using a very
  976. strange shell, this has the same result as `$(wildcard *.c)'.
  977.